package com.a16.yuejian.yuejian.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.a16.yuejian.yuejian.R;
import com.a16.yuejian.yuejian.activity.MyPostActivity;
import com.a16.yuejian.yuejian.javabean.Post;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by 王子恒 on 2017/11/11.
 */

public class MyPostAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List mDataList;
    private LayoutInflater mLayoutInflater;

    public MyPostAdapter(Context mContext, List mDataList){
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_postcardview,parent,false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final Post entity = (Post) mDataList.get(position);
        if (null == entity){
            return;
        }else {
            final ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.my_post_username.setText(entity.getUsername());
            viewHolder.my_post_comment.setText(entity.getPost());
            viewHolder.my_post_time.setText(entity.getTime());
            Picasso.with(mContext).load("http://10.0.2.2:8080/neu_yuejian201/public/photo/"+entity.getPostpic()).into(viewHolder.my_post_img);
            Picasso.with(mContext).load("http://10.0.2.2:8080/neu_yuejian201/public/photo/"+entity.getPic()).into(viewHolder.my_post_userpic);
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, MyPostActivity.class);
                    intent.putExtra("postid",entity.getPostid());
                    intent.putExtra("postpic",entity.getPostpic());
                    intent.putExtra("post",entity.getPost());
                    intent.putExtra("time",entity.getTime());
                    intent.putExtra("username",entity.getUsername());
                    intent.putExtra("pic",entity.getPic());
                    mContext.startActivity(intent);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        ImageView my_post_userpic,my_post_img;
        TextView my_post_username,my_post_time,my_post_comment;
        LinearLayout my_post_container;

        public ViewHolder(View itemView) {
            super(itemView);
            my_post_username = (TextView) itemView.findViewById(R.id.my_post_username);
            my_post_comment = (TextView) itemView.findViewById(R.id.my_post_comment);
            my_post_userpic = (ImageView) itemView.findViewById(R.id.my_post_userpic);
            my_post_img = (ImageView) itemView.findViewById(R.id.my_post_img);
            my_post_time = (TextView) itemView.findViewById(R.id.my_post_time);
            my_post_container= (LinearLayout)itemView.findViewById(R.id.my_post_container);
        }
    }

}
